<template>
  <div class="login-container">

    <div class="login-box">

     <login-header />


      <el-tabs v-model="activeName">
        <el-tab-pane label="账号登录" name="account">

          <el-form v-if="activeName === 'account'" ref="postForm" :model="postForm" :rules="loginRules">
            <el-form-item prop="username">
              <el-input
                v-model="postForm.username"
                style="width: 100%"
                placeholder="用户名"
                prefix-icon="el-icon-user"
              />
            </el-form-item>

            <el-form-item prop="password">
              <el-input
                v-model="postForm.password"
                show-password
                style="width: 100%"
                placeholder="密码"
                type="password"
                prefix-icon="el-icon-lock"
              />
            </el-form-item>

            <el-form-item prop="captchaValue">
              <yf-captcha v-model="postForm" />
            </el-form-item>

            <el-form-item>
              <el-button type="primary" :loading="loading" style="width: 100%" @click.native.prevent="accountLogin">登录</el-button>
            </el-form-item>

          </el-form>

        </el-tab-pane>
        <el-tab-pane label="手机登录" name="mobile">

          <el-form v-if="activeName === 'mobile'" ref="postForm" :model="postForm" :rules="loginRules">
            <el-form-item prop="mobile">
              <el-input
                v-model="postForm.mobile"
                style="width: 100%"
                placeholder="手机号码"
                prefix-icon="el-icon-mobile"
              />
            </el-form-item>

            <el-form-item prop="smsCode">
              <sms-input v-model="postForm.smsCode" :mobile="postForm.mobile" />
            </el-form-item>

            <el-form-item>
              <el-button type="primary" :loading="loading" style="width: 100%" @click.native.prevent="mobileLogin">登录</el-button>
            </el-form-item>

          </el-form>

        </el-tab-pane>
      </el-tabs>


      <div>
        <div style="display: flex">
          <div style="text-align: left; flex-grow: 1">
            <el-link type="primary"  href="/#/register">注册账号</el-link>
          </div>

          <div style="text-align: right; flex-grow: 1">
           <el-link type="default" href="/#/forgot">忘记密码？</el-link>
          </div>
        </div>
      </div>




    </div>

    <div class="footer" v-html="siteData.copyRight" />

  </div>

</template>

<script>
import { mapGetters } from 'vuex'
import YfCaptcha from '@/components/Captcha'
import SmsInput from './components/sms-input'
import LoginHeader from '@/views/login/components/login-header'

export default {
  components: { LoginHeader, SmsInput, YfCaptcha },
  data() {
    return {

      activeName: 'account',
      autoLogin: false,
      loading: false,
      postForm: {
        username: '',
        password: '',
        captchaKey: '',
        captchaValue: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', message: '用户名不能为空' }],
        password: [{ required: true, trigger: 'blur', message: '密码不能为空' }],
        captchaValue: [{ required: true, trigger: 'blur', message: '验证码不能为空' }],
        smsCode: [{ required: true, trigger: 'blur', message: '短信验证码不能为空' }]
      }
    }
  },
  computed: {
    ...mapGetters([
      'siteData'
    ])
  },

  methods: {

    loginBack() {
      // 其它跳到后台
      this.$router.push({ path: '/admin/dashboard' })

      setTimeout(function() {
        this.loading = false
      }, 1800)
    },

    mobileLogin() {
      this.$refs.postForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/mobileLogin', this.postForm)
            .then(() => {
              this.loginBack()
            })
            .catch(() => {
              this.loading = false
            })
        }
      })
    },

    accountLogin() {
      this.$refs.postForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/login', this.postForm)
            .then(() => {
              this.loginBack()
            })
            .catch(() => {
              this.loading = false
            })
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
@import "~@/styles/login.scss";

.el-divider__text{
  background: #f0f2f5;
  color: #999;
  font-size: 12px;
}
</style>
